<template>
  <div class="table">
    <div class="container">
      <div class="handle-box">
        <el-button
          type="primary"
          class="handle-del mr10"
          @click="handleDeleteBatch"
          size="mini"
          >批量删除</el-button
        >
        <el-input v-model="keyWord" placeholder="筛选部分用户"
          class="handle-input mr10"
          size="mini"
        ></el-input>
        <el-button type="primary" @click="addVisible = true" size="mini">添加用户</el-button>
      </div>
      <el-table :data="tableData" @selection-change="handleSelectionChange"
        border
        style="width: 100%"
        ref="multipleTable"
        height="450px"
        size="mini">
        <el-table-column type="selection" align="center"></el-table-column>
        <el-table-column label="头像" align="center" width="150">
          <template slot-scope="scope">
            <el-image v-if="scope.row.avatarUrl"  :src="getImageUrl(scope.row.avatarUrl)" style="height: 80px"></el-image>
            <i v-else class="el-icon-user" style="font-size: 50px;color: burlywood"></i>
          </template>
        </el-table-column>
        <el-table-column
          label="账号"
          prop="username"
          align="center"
        ></el-table-column>
        <el-table-column
          label="昵称"
          prop="nickname"
          align="center"
        ></el-table-column>
        <el-table-column
          label="是否被禁用"
          prop="isEnabled"
          :formatter="formatBoolean"
          align="center"
        >
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button @click="handleUpdate(scope.row)" type="text" size="small"
              >编辑</el-button
            >
            <el-button
              type="text"
              @click="handleDelete(scope.row)"
              size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination @current-change="handleCurrentChange"
          background
          layout="total, prev, pager, next"
          :current-page="current"
          :page-size="size"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>

    <!-- 添加弹出框 -->
    <el-dialog title="添加用户" :visible.sync="addVisible" width="700px" center>
      <el-container>
        <el-aside width="40%">
          <el-form ref="addForm" :model="addForm" :inline="true" label-width="100px">
            <el-form-item label="用户头像">
              <i class="el-icon-user" style="font-size: 100px;color: burlywood"></i>
            </el-form-item>
          </el-form>
        </el-aside>
        <el-main>
          <el-form ref="addForm" :model="addForm" :inline="true" label-width="100px">
            <el-form-item label="账号">
              <el-input v-model="addForm.username"></el-input>
            </el-form-item>
            <el-form-item label="昵称">
              <el-input v-model="addForm.nickname"></el-input>
            </el-form-item>
          </el-form>
        </el-main>
      </el-container>
      <el-form ref="addForm" :model="addForm" :inline="true" label-width="100px">
        <el-form-item label="密码">
          <el-input v-model="addForm.password" show-password></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="addForm.role" multiple placeholder="请选择">
            <el-option v-for="role in roleList"
              :key="role.id" :label="role.nameRn" :value="role.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否禁用">
          <el-radio-group v-model="addForm.isEnabled">
            <el-radio border :label="true">是</el-radio>
            <el-radio border :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取消</el-button>
        <el-button type="primary" @click="addRow">确定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="updateVisible" width="700px" center>
      <el-container>
        <el-aside width="40%">
          <el-form ref="updateForm" :model="updateForm" :inline="true" label-width="100px">
            <el-form-item label="用户头像">
              <el-upload class="avatar-uploader"
                :action="upload(updateForm.id)"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeImgUpload">
                <img :src="getImageUrl(updateForm.avatarUrl)" class="avatar"/>
              </el-upload>
            </el-form-item> </el-form
        ></el-aside>
        <el-main>
          <el-form ref="updateForm" :model="updateForm" :inline="true" label-width="100px">
            <el-form-item label="账号">
              <el-input v-model="updateForm.username"></el-input>
            </el-form-item>
            <el-form-item label="昵称">
              <el-input v-model="updateForm.nickname"></el-input>
            </el-form-item>
          </el-form>
        </el-main>
      </el-container>
      <el-form
        ref="updateForm"
        :model="updateForm"
        :inline="true"
        label-width="100px"
      >
        <el-form-item label="角色">
          <el-select v-model="updateForm.role" multiple placeholder="请选择">
            <el-option v-for="role in roleList"
              :key="role.id" :label="role.nameRn" :value="role.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否禁用">
          <el-radio-group v-model="updateForm.isEnabled">
            <el-radio border :label="true">否</el-radio>
            <el-radio border :label="false">是</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateVisible = false">取消</el-button>
        <el-button type="primary" @click="updateRow">确定</el-button>
      </span>
    </el-dialog>

    <!-- 删除提示框 -->
    <el-dialog title="提示" :visible.sync="delVisible" width="350px" center>
      <div class="del-dialog-cnt" align="center">
        删除不可恢复，是否确定删除？
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delVisible = false">取消</el-button>
        <el-button type="primary" @click="delRow">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mixin } from '../mixins'
import { SPACE, selectPage, selectPageByWord, insert, update, deleteByIds, selectList } from '../api/baseApi'
export default {
  mixins: [mixin],
  data() {
    return {
      size: 5,
      roleList: [],
      rules: {
        username: [
          { required: true, trigger: 'blur' }
        ],
        password: [
          { required: true, trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ],
        phoneNum: [
          { essage: '请选择日期', trigger: 'blur' }
        ],
        email: [
          { message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        birth: [
          { required: true, type: 'date', message: '请选择日期', trigger: 'change' }
        ],
        introduction: [
          { message: '请输入介绍', trigger: 'blur' }
        ],
        location: [
          { message: '请输入地区', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    // 计算当前表格中的数据
    data() {
      return this.tableData.slice((this.current - 1) * this.size, this.current * this.size)
    }
  },
  watch: {
    keyWord: function () {
      this.findRow()
    }
  },
  mounted() {
    this.findRow()
    this.findRoleList()
  },
  methods: {
    // 添加
    addRow() {
      insert(SPACE.user, this.addForm).then(res => {
        if (res.state === 200) {
          this.showSuccess()
          this.findRow()
          this.addVisible = false
        } else {
          this.showError()
        }
      })
    },
    // 禁用用户
    delRow() {
      deleteByIds(SPACE.user, this.ids).then(res => {
        if (res.state === 200) {
          this.showSuccess()
          this.findRow()
        } else {
          this.showError()
        }
      })
      this.delVisible = false
    },
    // 修改
    updateRow() {
      update(SPACE.user, this.updateForm).then(res => {
        if (res.state === 200) {
          this.showSuccess()
          this.findRow()
          this.updateVisible = false
        } else {
          this.showError()
        }
      })
    },
    // 查找角色列表
    findRoleList() {
      selectList(SPACE.role).then(res => {
        this.roleList = res.state === 200 ? res.data : []
      })
    },
    // 分页查询列表
    async findRow() {
      if (this.keyWord === '') {
        selectPage(SPACE.user, this.current, this.size).then(res => {
          this.tableData = res.state === 200 ? res.data.records : []
        })
      } else {
        selectPageByWord(SPACE.user, this.current, this.size, this.keyWord).then(res => {
          this.tableData = res.state === 200 ? res.data.records : []
        })
      }
    },
    upload(id) {
      return this.uploadUrl(SPACE.user, id)
    },
    handleAvatarSuccess(res) {
      this.updateForm.avatarUrl = res.data.avatarUrl
    }
  }
}

</script>

<style scoped>
.avatar-uploader .el-upload {
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 128px;
  height: 128px;
  line-height: 128px;
  text-align: center;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
}
.avatar {
  width: 128px;
  height: 128px;
  display: block;
}
</style>
